package com.atguigu.exam.exceptionhandlers;

import com.atguigu.exam.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice//获取全局的异常
@Slf4j
public class GlobalExceptionHandler {
    //定义异常处理的handler (与controller类似)
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        //打印日志
        e.printStackTrace();//错误的堆栈信息先打印!
        log.error("代码出现异常，异常信息为{}",e.getMessage());
        return Result.error(e.getMessage());
    }
}
